package com.fingard.dsp.bank.directbank.citic05;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.citic.openbank.configuration.OpenBankSdkConfiguration;
import com.citic.openbank.sdk.CiticSdk;
import com.citic.openbank.sdk.CiticSdkInterface;
import com.citic.openbank.sdk.RequestHeader;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;


public class CITIC05RealTime extends CITIC05Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);
        String ownAct = p_req9108.singleDetail.recAct;
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + ownAct + "]商户配置信息,请检查portfiles/" + p_req9108.reqHeader.bankCode + "/config.xml中'actSet'相关配置!";
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            CITIC05Header bankHeader = new CITIC05Header();
            bankHeader.openTransCode = "IFOPS005";
            bankHeader.openMerFlowId = p_req9108.singleDetail.askNumber;
            CiticSdkInterface sdk = CiticSdk.getInstance();
            setKeys(sdk, actItem);
            setConfiguration(sdk, actItem);
            RequestHeader header = new RequestHeader();
            writeCommonHeader(header, bankHeader, actItem);
            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("sndseqno", p_req9108.singleDetail.askNumber);
            jsonRequest.put("mccprtcno", actItem.recAgNum);
            jsonRequest.put("mrchname", actItem.userCode);
            jsonRequest.put("mrchtype", actItem.userFlag);
            jsonRequest.put("payname", p_req9108.singleDetail.payName);
            jsonRequest.put("payacc", p_req9108.singleDetail.payAct);
            jsonRequest.put("phone", p_req9108.singleDetail.cellPhone);
            String accFlag = "";
            String sameBnk = p_req9108.singleDetail.sameBnk;
            String isPrivate = p_req9108.singleDetail.isPrivate;
            if ("1".equals(sameBnk)) {
                if ("1".equals(isPrivate)) {
                    accFlag = "11";
                } else {
                    accFlag = "12";
                }
            } else {
                if ("1".equals(isPrivate)) {
                    accFlag = "21";
                } else {
                    accFlag = "22";
                }
            }
            jsonRequest.put("accflag", accFlag);
            jsonRequest.put("idtype", convertCertTypeToBank(p_req9108.singleDetail.certType));
            jsonRequest.put("idno", p_req9108.singleDetail.certNum);
            jsonRequest.put("amt", p_req9108.singleDetail.payAmount);
            jsonRequest.put("tfprtcno", p_req9108.singleDetail.authCode);

//            String tmpStrRet = sendToBank(header, jsonRequest.toJSONString());
            String tmpStrRet = sdk.remoteCall(header, jsonRequest.toJSONString());

            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                    JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                    String retCode = tmpJsonResp.getString("RETCODE");
                    String retMsg = tmpJsonResp.getString("RETMSG");
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.transState = retCode;
                    tmpRetDetail.payInfoCode = retCode;
                    tmpRetDetail.respInfo = retMsg;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应数据格式非法";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应为空";
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9144);
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req9144.recAct + "]商户配置信息!";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            CITIC05Header bankHeader = new CITIC05Header();
            bankHeader.openMerFlowId = getQryTransID();
            bankHeader.openTransCode = "IFOPS007";
            CiticSdkInterface sdk = CiticSdk.getInstance();
            setKeys(sdk, actItem);
            setConfiguration(sdk, actItem);
            RequestHeader header = new RequestHeader();
            writeCommonHeader(header, bankHeader, actItem);
            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("sndseqno", bankHeader.openMerFlowId);
            jsonRequest.put("mccprtcno", actItem.recAgNum);
            jsonRequest.put("orgseqno", p_req9144.askNumber);
            jsonRequest.put("trandt", Format.Date10Format.format(p_req9144.getBgnDateTime()));

//            String tmpStrRet = sendToBank(header, jsonRequest.toJSONString());
            String tmpStrRet = sdk.remoteCall(header, jsonRequest.toJSONString());

            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                    JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                    String retCode = tmpJsonResp.getString("RETCODE");
                    String retMsg = tmpJsonResp.getString("RETMSG");
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.transState = retCode;
                    tmpRetDetail.payInfoCode = retCode;
                    tmpRetDetail.respInfo = retMsg;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应数据格式非法";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应为空";
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
